package result

type OperationResultRes struct {
	ResultType int         `json:"resultType"    dc:"错误码：3成功，非3失败"`
	Message    string      `json:"message" dc:"消息内容"`
	Data       interface{} `json:"data"    dc:"结果信息"`
	Succeeded  bool        `json:"succeeded" dc:"是否成功"`
	Error      bool        `json:"error" dc:"是否错误"`
}

func (r *OperationResultRes) SuccessAll(data interface{}, msg string) {
	if msg == "" {
		msg = "操作成功"
	}

	r.ResultType = CodeOK.ResultType()
	r.Message = msg
	r.Data = data
	r.Succeeded = true
	r.Error = !r.Succeeded
}

func (r *OperationResultRes) Success(data interface{}) {
	r.SuccessAll(data, "")
}

func (r *OperationResultRes) SuccessNo() {
	r.Success(nil)
}
func (r *OperationResultRes) SuccessMsg(msg string) {
	r.SuccessAll(nil, msg)
}
func (r *OperationResultRes) Fail(msg string) {
	if msg == "" {
		msg = "操作失败"
	}

	r.ResultType = CodeInternalError.ResultType()
	r.Message = msg
	r.Data = nil
	r.Succeeded = false
	r.Error = !r.Succeeded
}

func (r *OperationResultRes) FailNo() {
	r.Fail("操作失败")
}

// SuccessAll 上面是给结构体定义的函数
// 下面是给包提定义的函数
func SuccessAll(data interface{}, msg string) *OperationResultRes {
	if msg == "" {
		msg = "操作成功"
	}
	return &OperationResultRes{CodeOK.ResultType(), msg, data, true, false}
}

func Success(data interface{}) *OperationResultRes {

	return SuccessAll(data, "")
}

func SuccessNo() *OperationResultRes {
	return Success(nil)
}

func SuccessMsg(msg string) *OperationResultRes {
	return SuccessAll(nil, msg)
}

func Fail(msg string) *OperationResultRes {
	if msg == "" {
		msg = "操作失败"
	}
	return &OperationResultRes{CodeInternalError.ResultType(), msg, nil, false, true}
}

func FailNo() *OperationResultRes {
	return Fail("操作失败")
}
